<template>
  <div class="editAddress">
    <van-nav-bar
        title="编辑地址"
        left-text="返回"
        left-arrow
        :placeholder="true"
        :safe-area-inset-top="true"
        @click-left="$router.back()"
    />
    <div class="container">
      <van-address-edit
          :area-list="areaList"
          show-postal
          show-delete
          show-set-default
          show-search-result
          :search-result="searchResult"
          :address-info="addressInfo"
          :area-columns-placeholder="['请选择', '请选择', '请选择']"
          @save="onSave"
          @delete="onDelete"
          @change-detail="onChangeDetail"
          save-button-text="保存地址"
          save-button-color="#0c34ba"
      />
      <div id="container"></div>
    </div>
  </div>
</template>

<script>
import {areaList} from "@vant/area-data";
import {deleteAddress, editAddress, findAddressByAid} from "@/api/address";
import {getSuggestion} from "@/api/map";

export default {
  name: "editAddress",
  data() {
    return {
      areaList,
      searchResult: [],
      tokenString: "",
      aid: "",
      addressInfo: {}
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      let self = this;
      self.aid = self.$route.query.aid;
      self.tokenString = this.$cookies.get("tokenString");
      let data = {
        tokenString: self.tokenString,
        aid: self.aid
      }
      findAddressByAid(data).then(res => {
        if (res.data.code === 700) {
          self.$toast.fail('登录状态异常，请重新登录！');
          self.$router.push('/login');
        } else if (res.data.code === 40000) {
          self.addressInfo = res.data.result[0]
          self.addressInfo.isDefault === 0 ? self.addressInfo.isDefault = false : self.addressInfo.isDefault = true
        } else {
          self.$toast.fail(res.data.msg);
          self.$router.back();
        }
      })
    },
    onSave(data) {
      let self = this;
      let datas = {
        appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
        tokenString: self.tokenString,
        aid: self.aid,
        name: data.name,
        tel: data.tel,
        province: data.province,
        city: data.city,
        county: data.county,
        addressDetail: data.addressDetail,
        areaCode: data.areaCode,
        postalCode: data.postalCode,
        isDefault: data.isDefault === true ? '1' : '0'
      }
      editAddress(datas).then(res => {
        if (res.data.code === 30000) {
          self.$toast.success(res.data.msg);
          self.$router.back();
        } else {
          self.$toast.fail('登录状态异常，请重新登录！');
          self.$router.push('/login');
        }
      })
    },
    onChangeDetail(val) {
      //如果该对象存在，那么地理位置服务可用。
      if (val === '') {
        return;
      }
      // 获取位置关键字提示
      getSuggestion(val).then(res => {
        this.searchResult = res.data
      })
    },
    onDelete() {
      let self = this;
      let data = {
        appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
        tokenString: self.tokenString,
        aid: self.aid
      }
      deleteAddress(data).then(res => {
        if (res.data.code === 700) {
          self.$toast.fail('登录状态异常，请重新登录！');
          self.$router.push('/login')
        } else if (res.data.code === 10000) {
          self.$toast.success({message: "res.data.msg", duration: 1500});
          self.$router.back();
        } else {
          self.$toast.fail(res.data.msg);
          self.$router.push('/')
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
/deep/ .van-button--danger {
  color: #fff;
  background-color: #0c34ba;
  border: 1px solid #0c34ba;
}
</style>
